home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Name Optional Parameters

If a routine takes optional parameters, use named arguments in the calling routine. That makes it obvious which arguments are included. It also means you do not need to carefully count all of the commas for omitted arguments to make sure you supply each value in its correct position. You can easily add, remove, and rearrange the arguments without messing up the comma count.

‘ Display text at the indicated spot using the
‘ specified font properties.
Private Sub PrintTextAt(X As Single, Y As Single, txt As String, _
    Optional font_name As String = “Times New Roman”, _
    Optional font_size As Single = 12, _
    Optional is_bold As Boolean = False, _
    Optional is_italic As Boolean = False, _
    Optional is_underlined As Boolean = False, _
    Optional is_struck_through As Boolean = False, _
    Optional fore_color As Long = vbBlack)

    ForeColor = fore_color
    Font.Name = font_name
    Font.Size = font_size
    Font.Bold = is_bold
    Font.Italic = is_italic
    Font.Underline = is_underlined
    Font.Strikethrough = is_struck_through
    CurrentX = X
    CurrentY = Y
    Print txt
End Sub

Without named arguments, the program could use the PrintTextAt subroutine as in the following code:

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)

    PrintTextAt X, Y, “www.vb-helper.com”, , , , , , , vbRed
End Sub

Now suppose you wanted to make the text appear italicized. It would be relatively difficult to modify this text correctly. You would need to look up the definition of subroutine PrintTextAt and carefully match up the parameters with the arguments already used in the Form_MouseUp event handler. You would then need to insert the new parameter in its correct position.

On the other hand, the following code is easy to modify.

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)

    PrintTextAt X:=X, Y:=Y, txt:=“www.vb-helper.com”, _
        fore_color:=vbRed
End Sub

To make this code italicize the text, you only need to add the is_italic named argument to the end of the parameter list.

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
    X As Single, Y As Single)

    PrintTextAt X:=X, Y:=Y, txt:=“www.vb-helper.com”, _
        fore_color:=vbRed, is_italic:=True
End Sub

Use named parameters when you call a subroutine, particularly if you omit some parameters or the parameter list is long.

Validate ParamArray Parameters

The ParamArray keyword lets you create a subroutine that takes any number of parameters. The parameter array is a variant array that must appear as the routine’s last parameter. For instance, the ComputeTotal function shown in the following code adds up its arguments and returns the total.

‘ Calculate the total of the routine’s arguments.
Public Function ComputeTotal(ParamArray values() As Variant) As Double
Dim i As Integer
Dim total As Double

    For i = LBound(values) To UBound(values)
        total = total + CDbl(values(i))
    Next i    ComputeTotal = total
End Function

ParamArray parameters are always variant arrays containing zero or more elements, each of which is a variant. Variants can contain all sorts of meaningless data, so a subroutine that uses a ParamArray must validate the number, types, and values of the parameters it receives. For instance, the ComputeTotal function should verify that its arguments are numbers.

Although it is possible for a routine to call ComputeTotal with zero or one parameter, this probably indicates a bug in the calling routine. To catch that bug as soon as possible, ComputeTotal should also check that the number of parameters is at least two. If it turns out that the program has a valid reason to pass fewer than two parameters to this function, it can be changed later. Until then, the routine looks for probable bugs by verifying that there are at least two parameters.

‘ Calculate the total of the routine’s arguments.
Public Function ComputeTotal(ParamArray values() As Variant) As Double
Dim i As Integer
Dim total As Double

    ‘Make sure there are at least 2 items in the
    ‘ ParamArray.
    If UBound(values) - LBound(values) + 1 < 2 Then Stop

    ‘ Verify that all ParamArray arguments are numeric.
    For i = LBound(values) To UBound(values)
        If Not IsNumeric(values(i)) Then Stop
    Next i

    ‘ Proceed with processing.
    For i = LBound(values) To UBound(values)
        total = total + CDbl(values(i))
    Next i
    ComputeTotal = total
End Function

Stop or Raise Errors When Appropriate

There are three main actions a routine can take when it receives a bad parameter:

•  Stop.
•  Raise an error.
•  Ignore the parameter and take some reasonable default action.

If the parameter makes no sense whatsoever, the program should stop so you can find the error. For example, if the function takes a variant parameter and it expects a string but receives an integer, it should stop. Then you can figure out why the calling routine is passing it an integer and fix it.

If a function is asked to return the average value of the items in an array and the array contains no items, it should stop. The function cannot take the average of zero items, so it should not pretend to do so. It should stop so you can fix the calling routine.

In each of these cases, the routine could raise an error. Then the calling routine could trap the error and ignore it. That would let the bug remain undetected in the calling routine. Call immediate attention to the error by stopping. It never makes sense to take an average of zero items so the routine should never continue.

There are other times when a value may be valid sometimes but is not currently. For instance, a subroutine may take a filename as an argument. The parameter may contain a valid filename, but the indicated file may not exist. In that case, the argument looks valid, but it is not useful to the program.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.